package com.tencent.iov.cowin.befsale.clue.service.cus.impl;

import com.github.pagehelper.Page;
import com.tencent.iov.cowin.befsale.clue.controller.cus.assembler.DealerCustomerFollowPlanAssembler;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.*;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerFollowPlanResponse;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.DealerCustomerFollowPlan;
import com.tencent.iov.cowin.befsale.clue.repository.cus.DealerCustomerFollowPlanManager;
import com.tencent.iov.cowin.befsale.clue.service.cus.DealerCustomerFollowPlanService;
import com.tencent.iov.cowin.befsale.clue.service.cus.DealerCustomerFollowRecordService;
import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.model.dao.PageRequest;
import com.software.boot.common.model.dao.PageResponse;
import com.software.boot.common.util.BeanUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.boot.common.util.ValidateUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.DeleteEnum;
import com.software.common.enums.SCRMRequestHeaderEnum;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Describe: 经销商跟进线索表_Service实现层
 * @author: xiamu
 * @created: 2021-12-21 17:29:54
 * @version: 1.0
 */
@Service
public class DealerCustomerFollowPlanServiceImpl implements DealerCustomerFollowPlanService {

    @Autowired
    private DealerCustomerFollowPlanManager dealerCustomerFollowPlanManager;
    @Autowired
    private DealerCustomerFollowRecordService dealerCustomerFollowRecordService;

    @Override
    public CustomerFollowPlanResponse getDetailById(Long id) {
        //验证请求参数
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        // 处理业务
        DealerCustomerFollowPlan dealerCustomerFollowPlan = dealerCustomerFollowPlanManager.getDetailById(id);
        if (EmptyUtils.isNotEmpty(dealerCustomerFollowPlan)) {
            return BeanUtil.copy(dealerCustomerFollowPlan, CustomerFollowPlanResponse.class);
        }
        //返回结果
        return null;
    }

    @Override
    public Long insert(AddCustomerFollowPlanRequest request) {
        //验证请求参数
        ValidateUtils.checkObjNull(request, "参数为null");
        ValidateUtils.checkLongGTZero(request.getClueId(), "线索ID为null");
        ValidateUtils.checkLongGTZero(request.getCusId(), "经销商客户ID为null");

        //处理业务
        DealerCustomerFollowPlanAssembler.initAddDealerCustomerFollowPlanRequest(request);
        DealerCustomerFollowPlan dealerCustomerFollowPlan = DealerCustomerFollowPlanAssembler.transformVOToPO(request, DealerCustomerFollowPlan.class);
        int result = dealerCustomerFollowPlanManager.insert(dealerCustomerFollowPlan);
        // 封装返回值
        if (result <= 0) {
            throw new BusinessException(NEError.BUSINESS_ERROR, "保存失败");
        }
        return dealerCustomerFollowPlan.getId();
    }

    @Override
    public int insertBatch(List<AddCustomerFollowPlanRequest> requests) {
        // 验证请求参数
        // 处理业务
        // 封装返回值
        List<DealerCustomerFollowPlan> followPlans = DealerCustomerFollowPlanAssembler.transformPOToResponseVoList(requests, DealerCustomerFollowPlan.class);
        return dealerCustomerFollowPlanManager.insertBatch(followPlans);
    }

    @Override
    public int update(UpdateCustomerFollowPlanRequest request) {

        // 验证请求参数
        //处理业务
        //返回对象
        DealerCustomerFollowPlanAssembler.initUpdateDealerCustomerFollowPlanRequest(request);
        DealerCustomerFollowPlan followPlan = DealerCustomerFollowPlanAssembler.transformVOToPO(request, DealerCustomerFollowPlan.class);
        return dealerCustomerFollowPlanManager.update(followPlan);
    }

    @Override
    public int delete(DeleteDealerCustomerFollowPlanRequest request) {
        Long id = request.getId();
        //验证请求参数
        ValidateUtils.checkObjNull(id, "请求参数不能为空");
        ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
        //处理业务
        DealerCustomerFollowPlan dealerCustomerFollowPlan = new DealerCustomerFollowPlan();
        dealerCustomerFollowPlan.setId(id);
        dealerCustomerFollowPlan.setDeleted(DeleteEnum.DETELED.getFlag());
        // 返回对象
        return dealerCustomerFollowPlanManager.update(dealerCustomerFollowPlan);
    }

    @Override
    public List<DealerCustomerFollowPlan> getListByParam(QueryCustomerFollowPlanRequest request) {
        //填充隔离参数
        request.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        request.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        request.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));
        //可以设置排序
        DealerCustomerFollowPlan followPlan = DealerCustomerFollowPlanAssembler.transformVOToPO(request, DealerCustomerFollowPlan.class);
        //处理业务
        List<DealerCustomerFollowPlan> queryList = dealerCustomerFollowPlanManager.getListByParam(followPlan);
        // 返回结果
        if (CollectionUtils.isNotEmpty(queryList)) {
            return BeanUtil.copyByList(queryList, DealerCustomerFollowPlan.class);
        }
        return new ArrayList<>();
    }

    @Override
    public PageResponse<DealerCustomerFollowPlan> getPageByParam(PageRequest<QueryCustomerFollowPlanRequest> request) {
        //验证请求参数
        ValidateUtils.checkObjNull(request, "请求参数不能为空");
        //可以设置排序 query.orderByCreatedTime(true);
        PageRequest<DealerCustomerFollowPlan> planPageRequest = DealerCustomerFollowPlanAssembler.transformVOToPOPage(request, DealerCustomerFollowPlan.class);

        //填充隔离参数
        DealerCustomerFollowPlan query = planPageRequest.getParamData();
        query.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        query.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        query.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));

        //处理业务
        RowBounds rowBounds = new RowBounds(planPageRequest.getPageNum(), planPageRequest.getPageSize());
        Page<DealerCustomerFollowPlan> queryPageList = dealerCustomerFollowPlanManager.getPageByParam(query, rowBounds);
        PageResponse<DealerCustomerFollowPlan> pageResponse = new PageResponse<>();
        pageResponse.converPageResponse(pageResponse, queryPageList, planPageRequest);
        pageResponse.setDataList(BeanUtil.copyByList(queryPageList.getResult(), DealerCustomerFollowPlan.class));
        //返回结果
        return pageResponse;
    }

    @Override
    public CustomerFollowPlanResponse getOneByParam(QueryCustomerFollowPlanRequest request) {
        DealerCustomerFollowPlan followPlanRequest = DealerCustomerFollowPlanAssembler.transformVOToPO(request, DealerCustomerFollowPlan.class);
        //填充隔离参数
        followPlanRequest.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        followPlanRequest.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        followPlanRequest.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));

        DealerCustomerFollowPlan followPlan = dealerCustomerFollowPlanManager.getOneByParam(followPlanRequest);
        return EmptyUtils.isNotEmpty(followPlan) ? BeanUtil.copy(followPlan, CustomerFollowPlanResponse.class) : null;
    }

    /**
     * 根据条件统计计划表数量
     * add by zly
     *
     * @param clueIntentionGradeDataRequest 参数
     * @return 条件统计计划表数量
     */
    @Override
    public Integer getFollowUpData(ClueIntentionGradeDataRequest clueIntentionGradeDataRequest) {
        return dealerCustomerFollowPlanManager.getFollowUpData(clueIntentionGradeDataRequest);
    }
}
